package edu.ycxy.poetry.controller;

import edu.ycxy.poetry.entity.User;
import edu.ycxy.poetry.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * @Author @xin
 * @createDate 2022/4/27
 * @Description
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private IUserService userService;

    @PostMapping("/login")
    public String ManagerLogin(@RequestBody User user, HttpSession session){
        String login = userService.login(user);
        log.debug(login);
        if (login.equals("ok")){
            session.setAttribute("email",user.getEmail());
            //session.setAttribute("pwd",user.getPassword());
            return "success";
        }else if (login.equals("该用户不存在")){
            return "1";
        }else {
            return "2";
        }

    }

    //退出登录,清除session
    @RequestMapping("/signOut")
    public void SignOut(HttpSession session){
        session.removeAttribute("email");
        //session.removeAttribute("pwd");
    }

    //更新信息
    @RequestMapping("/update")
    public void updateInfo(@RequestBody User user){
        log.debug("获取到的user对象： " + user);
        userService.updateInfo(user);
    }

    @RequestMapping("/info")
    public User getInfo(@RequestBody User user){
        return userService.findInfo(user);
    }
}
